package com.liuya.safe.controller;

import com.liuya.base.BaseController;
import com.liuya.safe.client.SafeClient;
import com.liuya.safe.client.SafeWebClient;
import com.liuya.safe.model.SafePrivilege;
import com.liuya.safe.model.SafeUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
@Api(value = "设计器主控制器", description = "设计器主控制器")
@RequestMapping(value = "designerMain")
public class DesignerMainContrller extends BaseController {

    @ApiOperation(value = "设计器主页方法")
//    @RequiresPermissions("sys.designerMain.designer")
    @RequestMapping(value = "/designer")
    public String designer(HttpServletRequest req, ModelMap modelMap){
        Token token = new Token();
        // Is security enabled?
        boolean secured = InitController.isSecured();

        if (secured) {
            SafeUser currentUser = SafeWebClient.getCurrentUser(req);
            token.setCanAssignRoleToUser(SafeClient.hasPrivilege(SafePrivilege.ASSIGN_ROLE_TO_USER_ID, currentUser));
            token.setCanAdminRole(SafeClient.hasPrivilege(SafePrivilege.ROLE_ADMIN_ID, currentUser));
            token.setCanAdminPolicy(SafeClient.hasPrivilege(SafePrivilege.POLICY_ADMIN_ID, currentUser));
        }

        req.setAttribute("token", token);
        return "mainDesigner";
    }

    /**
     * Only used in designer.jsp. So there's no need extract it as a 'normal'
     * class
     *
     *
     */
    public class Token {
        private boolean canAssignRoleToUser = true;
        private boolean canAdminRole = true;
        private boolean canAdminPolicy = true;

        public boolean isCanAssignRoleToUser() {
            return canAssignRoleToUser;
        }

        public void setCanAssignRoleToUser(boolean canAssignRoleToUser) {
            this.canAssignRoleToUser = canAssignRoleToUser;
        }

        public boolean isCanAdminRole() {
            return canAdminRole;
        }

        public void setCanAdminRole(boolean canAdminRole) {
            this.canAdminRole = canAdminRole;
        }

        public boolean isCanAdminPolicy() {
            return canAdminPolicy;
        }

        public void setCanAdminPolicy(boolean canAdminPolicy) {
            this.canAdminPolicy = canAdminPolicy;
        }
    }

}
